//index.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js');
const api = require('../../utils/api.js');
Page({
	data: {
		order:{},
		courseMoney:0,//课程金额
		payTotal:"",//支付金额
		point:0,//账户余额
		restTime:0,//倒计时毫秒数
		formatSeconds:"",//倒计时
		ld:0,//遛点兑换人民币
		courseMoney:0,//课程金额
		disabled:false,
		items:[{value:0},{value:1}],
		payType:-1,//支付类型
		orderId:"",//订单id
		redBagId:null,//红包id
		redBagMoney:0,//红包金额
		pageNum:1,
		total:0,
	},
	onLoad: function(options) {
		this.setData({
			orderId:options.id,
		})
		this.findLd();
		this.getUserPoint(app.globalData.userId);
		this.getOrderInfo(options.id);
	},
	onShow:function(){
		if(this.data.redBagId != null){
			this.setData({
				payTotal:this.data.payTotal - this.data.redBagMoney,
			})
		}
	},
	//查询可用红包数量
	getTotal(coursePoint){
		let _this = this;
		api.redList({
			data: {
				ownerId: app.globalData.userId,
				pageSize: "20",
				pageNum: this.data.pageNum,
				coursePoint:coursePoint
			},
			success: function(res) {
				if(res.data.code == 0) {
					_this.setData({
						total:res.data.list.total
					});
				}
			},
		});
	},
	//选择一个红包
	toPackage(){
		wx.navigateTo({
			url: '../fetchred/selectlist?coursePoint=' + this.data.order.coursePoints + '&redBagId=' + this.data.redBagId
		})
	},
	getOrderInfo:function(id){
		let _this = this;
		api.orderInfo({
			data: {
				id:id
			},
			success: function(res) {
				if(res.data.code == 0) {
					let curDate = new Date().getTime();
					let orderDate = new Date(res.data.obj.createTime).getTime();
					if(orderDate + 15 * 60 * 1000 < curDate){
						//过期
						_this.setData({
							disabled: true,
						});
					}else{
						_this.setData({
							restTime: 15 * 60 * 1000 + orderDate - curDate,
							formatSeconds:util.formatSeconds(15 * 60 * 1000 + orderDate - curDate)
						});
						let timeInterval = setInterval(function(){
							if(_this.data.restTime - 1000 < 0){
								clearInterval(timeInterval);
								_this.setData({
									disabled: true,
								});
							}else{
								let restTime = _this.data.restTime - 1000;
								_this.setData({
									restTime: restTime,
									formatSeconds:util.formatSeconds(restTime)
								});
							}
						},1000);
					}
					_this.getTotal(res.data.obj.coursePoints);
					_this.setData({
						order:res.data.obj,
						courseMoney:_this.data.ld * res.data.obj.coursePoints,
						payTotal:_this.data.ld * res.data.obj.coursePoints,
					})
				}
			},
		});
	},
	//查询账户余额
	getUserPoint:function(ownerId){
		let _this = this;
		api.getUserPoint({
			data: {
				userId: ownerId,
			},
			success: function(res) {
				if(res.data.code == 0) {
					let point = res.data.point;
					_this.setData({
						point: point == null?0:point,
					});
				}
			},
		});
	},
	//查询遛点配置
	findLd() {
		let _this = this;
		api.getMateList({
			data: {
				pageSize: "10",
				pageNum: "1",
				type: "liudian"
			},
			success: function(res) {
				if(res.data.code == 0){
					_this.setData({
						ld: res.data.list.rows[0].value
					});
				}
			}
		});
	},
	radioChange: function(e) {
		if(e.detail.value == 0 ){
			if(this.data.point < this.data.courseMoney){
				wx.showModal({
					 title: '提示',
					 content: "余额不足",
					 showCancel:false,
					 success: function(res) {
					  
					 }
				})  
		        this.setData({
					payType: -1
				}); 
			}else{
				this.setData({
					payType: e.detail.value
				});
			}
		}else{
			this.setData({
				payType: e.detail.value
			});
		}
	   
	},
	pay(){
		let _this = this;
		if(this.data.payType == 0){
			this.paySuccess();
		}else{
			api.topay({
				data: {
					orderNo: this.data.order.orderNo,
					mobile: this.data.order.parentPhone,
					userId: app.globalData.userId,
				},
				success: function(res) {
					if(res.data.code == 0){
						wx.requestPayment({
						   'timeStamp': res.data.timestamp,
						   'nonceStr': res.data.noncestr,
						   'package': 'prepay_id=' + res.data.prepayid ,
						   'signType': 'MD5',
						   'paySign': res.data.sign,
						   'success':function(res){
							   _this.paySuccess();
						   },
						   'fail':function(res){
							   wx.showModal({
								  title: '提示',
								  content: "支付失败",
								  showCancel:false,
								  success: function(res) {
									  
								  }				
							   });
						   }
						});
					}else{
						wx.showModal({
							 title: '提示',
							 content: "支付失败",
							 showCancel:false,
							 success: function(res) {
							  
							 }				
						});
					}
				}
			});
		}
	},
	paySuccess(){
		api.paySuccess({
			data: {
				payType: this.data.payType,
				payTotal: this.data.payTotal,
				redBagId: this.data.redBagId,
				id:this.data.orderId,
			},
			success: function(res) {
				if(res.data.code == 0){
					wx.showModal({
						 title: '提示',
						 content: "支付成功",
						 showCancel:false,
						 success: function(res) {
							 wx.switchTab({
								url: "../order/order"
							})
						 }				
					});
				}else{
					wx.showModal({
						 title: '提示',
						 content: "支付失败",
						 showCancel:false,
						 success: function(res) {
						  
						 }				
					});
				}
			}
		});
	}
})